package cn.etg.day06;

import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * 读取当前项目根目录下所有后缀为.obj的文件，将这些Emp对象读取出来
 * 并存入到一个List集合中，然后按照员工工资从多到少的顺序依次输出员工信息
 * @author Xiloer
 *
 */
public class Test04 {
    public static void main(String[] args) {
        File file = new File("./src/api_homework/day06/b/");
        File[] files = file.listFiles();
        FileInputStream fis = null;
        ObjectInputStream ois = null;

        List<Emp> list = new ArrayList<Emp>();
        for (File f : files) {
            try {
                fis = new FileInputStream(f);
                ois = new ObjectInputStream(fis);

                Emp emp = (Emp)ois.readObject();
                list.add(emp);
            } catch (ClassNotFoundException | IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        Comparator<Emp> c = new Comparator<Emp>() {
            @Override
            public int compare(Emp o1, Emp o2) {
                return o2.getSalary() - o1.getSalary();
            }
        };
        list.sort(c);
        System.out.println(list);
    }
}
